#include "mycoin.h"
#include "QPainter"
//mycoin::mycoin(QWidget *parent) : QPushButton(parent)
//{


//}

mycoin::mycoin(QString btnImg){
    this->posx=0;
    this->posy=0;
    this->flag=0;
    this->isWin=false;
    this->isAnimation=false;
     QPixmap pix;
    bool ret=pix.load(btnImg);
    if(!ret){
        return;
    }
    //设置按钮固定大小
    this->setFixedSize( pix.width(),pix.height());

    //设置不规则图片样式，这里选择无边框
    this->setStyleSheet("QPushButton{border:0px;}");

    //设置图标
    this->setIcon(pix);

    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));

    timer1=new QTimer(this);
    timer2=new QTimer(this);
    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->min++);
        pix.load(str);
        //设置按钮固定大小
        this->setFixedSize( pix.width(),pix.height());
        //设置不规则图片样式，这里选择无边框
        this->setStyleSheet("QPushButton{border:0px;}");
        //设置图标
        this->setIcon(pix);
        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min>this->max){
            this->min=1;
            this->isAnimation=false;
            timer1->stop();
        }
    });
    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->max--);
        pix.load(str);
        //设置按钮固定大小
        this->setFixedSize( pix.width(),pix.height());
        //设置不规则图片样式，这里选择无边框
        this->setStyleSheet("QPushButton{border:0px;}");
        //设置图标
        this->setIcon(pix);
        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min>this->max){
             this->isAnimation=false;
            this->max=8;
            timer2->stop();
        }
    });
    this->setFocusPolicy(Qt::NoFocus);
}
void mycoin::changFlag(){
    if(this->flag){
        timer1->start(30);
        this->isAnimation=true;
        this->flag=false;
    }
    else{
          timer2->start(30);
          this->isAnimation=true;
        this->flag=true;
    }
}
void mycoin::mousePressEvent(QMouseEvent *ev){
    if(this->isAnimation||this->isWin){
        return;
    }
    return QPushButton::mousePressEvent(ev);
}
